package xin.clips.web.controller.gy;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.DateUtils;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.GyUserShareDoc;
import xin.clips.system.service.gy.IGyUserDocService;
import xin.clips.system.service.gy.IGyUserShareDocService;

/**
 * 用户分享的文档Controller
 *
 * @author clips
 * @date 2023-03-07
 */
@Controller
@RequestMapping("/gy/userShareDoc")
public class GyUserShareDocController extends BaseController
{
    private String prefix = "gy/userShareDoc";

    @Autowired
    private IGyUserShareDocService gyUserShareDocService;


    @RequiresPermissions("gy:userShareDoc:view")
    @VisistLog(title = "用户分享的文档")
    @GetMapping()
    public String userShareDoc()
    {
        return prefix + "/userShareDoc";
    }

    /**
     * 查询用户分享的文档列表
     */
    @RequiresPermissions("gy:userShareDoc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyUserShareDoc gyUserShareDoc)
    {
        startPage();
//        if(ShiroUtils.getUserId() != 1 && ShiroUtils.getUserId() != 3 ){
//            gyUserShareDoc.setUserId(ShiroUtils.getUserId());
//        }
        gyUserShareDoc.setUserId(ShiroUtils.getUserId());
        List<GyUserShareDoc> list = gyUserShareDocService.selectGyUserShareDocList(gyUserShareDoc);

//        for (GyUserShareDoc userShareDoc:list) {
//            if(userShareDoc.getValidState().toString().equals("0")){  //7天
//                int mix=DateUtils.differentDaysByMillisecond(userShareDoc.getValidTime(),DateUtils.getNowDate());
//                if(mix>=7){
//                    userShareDoc.setValidStr("已过期");
//                }else{
//                    userShareDoc.setValidStr("7天");
//                }
//            }else if(userShareDoc.getValidState().toString().equals("1")){
//                int mix=DateUtils.differentDaysByMillisecond(userShareDoc.getValidTime(),DateUtils.getNowDate());
//                if(mix>=30){
//                    userShareDoc.setValidStr("已过期");
//                }else{
//                    userShareDoc.setValidStr("30天");
//                }
//            }else if(userShareDoc.getValidState().toString().equals("2")){
//                userShareDoc.setValidStr("永久");
//            }
//
//
//        }
        return getDataTable(list);
    }

    /**
     * 导出用户分享的文档列表
     */
    @RequiresPermissions("gy:userShareDoc:export")
    @Log(title = "用户分享的文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyUserShareDoc gyUserShareDoc)
    {
        List<GyUserShareDoc> list = gyUserShareDocService.selectGyUserShareDocList(gyUserShareDoc);
        ExcelUtil<GyUserShareDoc> util = new ExcelUtil<GyUserShareDoc>(GyUserShareDoc.class);
        return util.exportExcel(list, "用户分享的文档数据");
    }

    /**
     * 新增用户分享的文档
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户分享的文档
     */
    @RequiresPermissions("gy:userShareDoc:add")
    @Log(title = "用户分享的文档", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyUserShareDoc gyUserShareDoc)
    {

        return toAjax(gyUserShareDocService.insertGyUserShareDoc(gyUserShareDoc));
    }

    /**
     * 分享文件
     */
    @GetMapping("/shareDoc/{fileId}")
    public String shareDoc(@PathVariable("fileId") String fileId, ModelMap mmap) {
        GyUserShareDoc userShareDoc = gyUserShareDocService.selectGyUserShareDocByFIleId(fileId);
        if(userShareDoc!=null){

            mmap.put("filePath",userShareDoc.getUrlInfo());
            mmap.put("fileName",userShareDoc.getFileName());
            mmap.put("result","ok" );
        }else{
            mmap.put("filePath","");
            mmap.put("fileName","");
            mmap.put("result","fail" );
        }
        return prefix + "/shareDoc";
    }

    /**
     * 修改用户分享的文档
     */
    @RequiresPermissions("gy:userShareDoc:edit")
    @GetMapping("/edit/{shareId}")
    public String edit(@PathVariable("shareId") Long shareId, ModelMap mmap)
    {
        GyUserShareDoc gyUserShareDoc = gyUserShareDocService.selectGyUserShareDocByShareId(shareId);
        mmap.put("gyUserShareDoc", gyUserShareDoc);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户分享的文档
     */
    @RequiresPermissions("gy:userShareDoc:edit")
    @Log(title = "用户分享的文档", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyUserShareDoc gyUserShareDoc)
    {
        return toAjax(gyUserShareDocService.updateGyUserShareDoc(gyUserShareDoc));
    }

    /**
     * 删除用户分享的文档
     */
    @RequiresPermissions("gy:userShareDoc:remove")
    @Log(title = "用户分享的文档", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gyUserShareDocService.deleteGyUserShareDocByShareIds(ids));
    }

    /**
     *  取消分享
     */
    @RequiresPermissions("gy:userShareDoc:edit")
    @PostMapping("/cancelShare")
    @ResponseBody
    public AjaxResult cancelShare(GyUserShareDoc gyUserShareDoc) {
        return toAjax(gyUserShareDocService.deleteGyUserShareDocByShareId(gyUserShareDoc.getShareId()));
    }

}
